package com.toogu.mazhangmen.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.toogu.mazhangmen.R;


public class MonitorView extends LinearLayout implements View.OnClickListener {

    private View  rootView;
    private Monitor monitorView;
    private TextView title;
    private TextView message;
    private View touch;

     private View.OnClickListener onClickListener;

    public MonitorView(Context context) {
        super(context);
        init(context);
    }

    public MonitorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MonitorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        rootView = LayoutInflater.from(context).inflate(R.layout.view_monitor, null, false);
        monitorView = rootView.findViewById(R.id.MonitorView);
        title = rootView.findViewById(R.id.monitorName);
        message = rootView.findViewById(R.id.message);
        touch = rootView.findViewById(R.id.touch);
        touch.setOnClickListener(this);
        addView(rootView,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public void onClick(View v) {
        if(onClickListener!=null) onClickListener.onClick(v);
    }

    public void setTitle(String titleName) {
        title.setText(titleName);
    }

    public void setIsConnected() {
        message.setText("");
        rootView.setBackgroundResource(R.color.blue);
        message.setVisibility(View.INVISIBLE);
    }

    public void setConnecting() {
        rootView.setBackgroundResource(R.color.yellow);
        message.setVisibility(View.VISIBLE);
        message.setText("连接中...");
    }

    public void setErrorMessage(String errorMessage) {
        if(errorMessage==null) {
            errorMessage= (String) message.getTag();
            if(errorMessage==null) errorMessage="连接中...";
        }
        rootView.setBackgroundResource(R.color.yellow);
        message.setVisibility(View.VISIBLE);
        message.setTag(errorMessage);
        message.setText(errorMessage);
    }

    public void setHelp() {
        rootView.setBackgroundResource(R.color.red);
    }

    public void closeHelp() {
        if(message.getText().toString().equals(""))rootView.setBackgroundResource(R.color.blue);
        else rootView.setBackgroundResource(R.color.yellow);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = width/9*5;
        setMeasuredDimension(width, height);
    }

    public RelativeLayout getMonitorView(){
        return monitorView;
    }


}